package com.haibei.adapter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.haibei.R;

public class ExpandListSiftAdapter extends BaseExpandableListAdapter {

	private Context mContext;
	private ArrayList<SiftBean> mArrayList;
	private static HashSet<SiftBean> mHashSet;
	private Set<String> mSetExpand;
	private OnSelectChangeListener onSelectChangeListener;
	static {
		mHashSet = new HashSet<ExpandListSiftAdapter.SiftBean>();
	}

	public ExpandListSiftAdapter(Context mContext, ArrayList<SiftBean> mArrayList) {
		this.mContext = mContext;
		this.mArrayList = mArrayList;
		this.mSetExpand = new HashSet<String>();
	}

	@Override
	public int getGroupCount() {
		return mArrayList.size();
	}

	public void clearSelect() {
		mHashSet.clear();
		notifyDataSetChanged();
	}

	public HashSet<SiftBean> getSelect() {
		return mHashSet;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mArrayList.get(groupPosition).getSubList().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mArrayList.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return mArrayList.get(groupPosition).getSubList().get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return groupPosition + childPosition;
	}

	public void expands(String index) {
		if (mSetExpand.contains(index)) {
			mSetExpand.remove(index);
		} else {
			mSetExpand.add(index);
		}
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	public OnSelectChangeListener getOnSelectChangeListener() {
		return onSelectChangeListener;
	}

	public void setOnSelectChangeListener(OnSelectChangeListener onSelectChangeListener) {
		this.onSelectChangeListener = onSelectChangeListener;
	}

	public void select(SiftBean select) {
		if (mHashSet.contains(select)) {
			mHashSet.remove(select);
		} else {
			mHashSet.add(select);
		}
		notifyDataSetChanged();
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		ViewHolderGroup vhg = null;
		if (null == convertView) {
			convertView = View.inflate(mContext, R.layout.item_sift_group, null);
			vhg = new ViewHolderGroup();
			vhg.imageView = (ImageView) convertView.findViewById(R.id.imageViewHook);
			vhg.textViewParent = (TextView) convertView.findViewById(R.id.textViewLocation);
			convertView.setTag(vhg);
		} else {
			vhg = (ViewHolderGroup) convertView.getTag();
		}
		SiftBean sb = mArrayList.get(groupPosition);
		vhg.textViewParent.setText(sb.getSiftString());
		if (mSetExpand.contains(String.valueOf(groupPosition))) {
			vhg.imageView.setImageResource(R.drawable.icon_expand_arrow_down);
		} else {
			vhg.imageView.setImageResource(R.drawable.icon_expand_arrow_right);
		}
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolderSub vhg = null;
		if (null == convertView) {
			convertView = View.inflate(mContext, R.layout.item_sift_sub, null);
			vhg = new ViewHolderSub();
			vhg.imageViewSub = (ImageView) convertView.findViewById(R.id.imageViewHookSub);
			vhg.textViewParentSub = (TextView) convertView.findViewById(R.id.textViewLocationSub);
			convertView.setTag(vhg);
		} else {
			vhg = (ViewHolderSub) convertView.getTag();
		}
		// vhg.imageViewSub
		final SiftBean sb = mArrayList.get(groupPosition).getSubList().get(childPosition);
		if (mHashSet.contains(sb)) {
			vhg.imageViewSub.setImageResource(R.drawable.bg_radio_check);
		} else {
			vhg.imageViewSub.setImageResource(R.drawable.bg_radio_uncheck);
		}
		vhg.textViewParentSub.setText(sb.getSiftString());
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				select(sb);
				if(null!=onSelectChangeListener){
					onSelectChangeListener.OnSelectChange(getSelect());
				}
			}
		});
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	// 选择更改
	public static interface OnSelectChangeListener {
		public void OnSelectChange(HashSet<SiftBean> siftBeans);
	}

	class ViewHolderGroup {

		TextView textViewParent;
		ImageView imageView;
	}

	class ViewHolderSub {
		TextView textViewParentSub;
		ImageView imageViewSub;
	}

	// ====
	public static class SiftBean {

		private String siftValue;
		private String siftString;
		private boolean mBooleanSelect;
		private ArrayList<SiftBean> subList;
		private int type;
		private String paramKey = "";
		public static final int type_brand = 0;
		public static final int type_region = 1;

		public SiftBean() {
			subList = new ArrayList<ExpandListSiftAdapter.SiftBean>();
		}

		public SiftBean(int type, String paramKey) {
			subList = new ArrayList<ExpandListSiftAdapter.SiftBean>();
			this.type = type;
			this.paramKey = paramKey;
		}

		public String getParamKey() {
			return paramKey;
		}

		public void setParamKey(String paramKey) {
			this.paramKey = paramKey;
		}

		public int getType() {
			return type;
		}

		public void setType(int type) {
			this.type = type;
		}

		public String getSiftValue() {
			return siftValue;
		}

		public void setSiftValue(String mStringSiftValue) {
			this.siftValue = mStringSiftValue;
		}

		public String getSiftString() {
			return siftString;
		}

		public void setSiftString(String mStringSiftString) {
			this.siftString = mStringSiftString;
		}

		public boolean isSelect() {
			return mBooleanSelect;
		}

		public void setSelect(boolean mBooleanSelect) {
			this.mBooleanSelect = mBooleanSelect;
		}

		public ArrayList<SiftBean> getSubList() {
			return subList;
		}

		public void setSubList(ArrayList<SiftBean> subList) {
			this.subList = subList;
		}

		public void addSiftBean(SiftBean siftBean) {
			this.subList.add(siftBean);
		}
	}
}
